#!/usr/bin/python
# -*- coding: utf-8 -*-

# External imports
import ConfigParser

# Internal imports
import cryptogram


class NotImplementedError(Exception):
  pass


class Reader(object):

  def __init__(self):
    super(Reader, self).__init__()
    self.__cryptograms = None

  def load(self, filenames):
    raise NotImplementedError

class ConfigReader(Reader):

  def __init__(self):
    super(ConfigReader, self).__init__()
    self.__config = ConfigParser.SafeConfigParser()

  def load(self, filenames):
    redFilenames = self.__config.read(filenames)
    self.__cryptograms = {}

    # Each section is a cryptogram
    for section in self.__config.sections():
      try:
        msg = self.__config.get(section, 'msg')
      except ConfigParser.NoOptionError:
        continue
      try:
        key = self.__config.get(section, 'key')
      except ConfigParser.NoOptionError:
        key = None

      self.__cryptograms[section.decode('utf-8')] = \
          cryptogram.Cryptogram(section.decode('utf-8'),
              msg.decode('utf-8'),
              key and key.decode('utf-8') or None)

  def get(self, cryptogramName):
    return self.__cryptograms.get(cryptogramName)

  def cryptograms(self):
    return self.__cryptograms.values()

